package com.linedata.acq.metier.norme.asql;

import gudusoft.gsqlparser.TGSqlParser;
import gudusoft.gsqlparser.TStatementList;

public class ASQL_NOR6 {

	private String		error	= "";
	private String		warning	= "";
	// private String source = "";
	private TGSqlParser	sqlparser;

	// private TCustomSqlStatement sql;

	public ASQL_NOR6(TGSqlParser sqlparser, String source) {
		this.sqlparser = sqlparser;
		// this.sql = sqlparser.sqlstatements.get(0);
		// this.source = source;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error += error;
	}

	public String getWarning() {
		return warning;
	}

	public void setWarning(String warning) {
		this.warning = warning;
	}

	/**
	 * EKPL014_1 :: Le script SQL commence par un BEGIN et se termine par END
	 */
	public void beginEndScript() {

		int cpt = 0;

		TStatementList sqlStatements = sqlparser.sqlstatements;
		for (int i = 0; i < sqlStatements.size(); i++) {

			if (sqlStatements.get(i).sqlstatementtype.toString().equals("sstsqlpluscmd")) {

				if (!sqlStatements.get(i).toString().startsWith("REM")
						&& !sqlStatements.get(i).toString().startsWith("/")
						&& !sqlStatements.get(i).toString().toUpperCase().startsWith("SET SERVEROUTPUT ON;")) {

					this.setError("\tEKPL014_1 ::\t" + sqlStatements.get(i));
				}
			}

			if (!sqlStatements.get(i).sqlstatementtype.toString().equals("sstplsql_block")
					&& !sqlStatements.get(i).sqlstatementtype.toString().equals("sstsqlpluscmd")) {

				this.setError("\tEKPL014_1 ::\t" + sqlStatements.get(i));
			}

			if (sqlStatements.get(i).sqlstatementtype.toString().equals("sstplsql_block")) {

				cpt++;
			}
		}

		if (cpt < 1) {

			this.setError("\tEKPL014_1 ::\t" + cpt + " blocs Begin/End dans le script\n");
		}
	}

	@Override
	public String toString() {

		return this.getError();
	}
}
